package com.mach.platform.sprider.getter;

import com.mach.platform.sprider.Conf;
import com.mach.platform.sprider.getter.DataLogService;
import com.mach.platform.util.SqlHelper;
import com.mach.platform.util.U;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class CmdMainWork implements Runnable{


    private  SqlHelper sqlHelper = null;
    private DataLogService dataLogService = null;

    public static CmdMainWork self;

    public CmdMainWork(){
        self = this;
        initBus();
    }

    public static void commitCmd(Integer devId, String cmd, String param) throws SQLException {

        if(param == null)param = "";
        self.sqlHelper.update("insert into iot_dev_cmd_log(cmd_time,dev_id,cmd,param)" +
                "value(now()," +
                " "+devId+"," +
                " '"+cmd+"'," +
                " '"+param+"')");
    }

    private void initBus(){
//        Mysql数据库
        this.sqlHelper = DataCenter.sqlHelper;
        //DataLogService
        dataLogService = Conf.getMongoDbHelper();
//        dataLogService = NissanDataLogService.get(MongoDbJdbc.get());
    }

    @Override
    public void run() {
        int cnt = 0;
        while (true){
            ++cnt;
           try{

               this.work();
               Thread.sleep(1000*5);


           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }

    private void cleanWin() {
//        select date_add(@dt, interval 1 day); -- add 1 day
//        select date_add(@dt, interval 1 hour); -- add 1 hour
//        select date_add(@dt, interval 1 minute); -- ...
//        select date_add(@dt, interval 1 second);
//        select date_add(@dt, interval 1 microsecond);
//        select date_add(@dt, interval 1 week);
//        select date_add(@dt, interval 1 month);
//        select date_add(@dt, interval 1 quarter);
//        select date_add(@dt, interval 1 year);

//        try {
//            this.sqlHelper.update("DELETE from iot_dev_data_win \n" +
//                    " where the_time<date_sub(now(), interval 7 day) ");
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }

    private List<GeneralDev> preDevs = new LinkedList<GeneralDev>();

    //开始抓取数据zzzzzzz
    public void work() {

        String sql2GetDevs = "select dev.id devId, dev.name name,type.prol prol,  dev.dev_type_id devTypeId,\n" +
                " dev.comm_addr commAddr,\n" +
                " cmdLog.id cmdId,cmdLog.cmd_time cmtTime,cmdLog.dev_id devId, \n" +
                " cmdLog.cmd cmd, cmdLog.param cmdParam, \n" +
//                " coll.id collId, coll.name collName," +
//                " coll.value_addr collAddr, coll.value_addr_str collAddrStr,\n " +
//                " coll.data_len collLen, coll.data_type collDataType,\n" +
                " '' \n" +
                " from iot_dev_cmd_log cmdLog\n" +
                " LEFT JOIN iot_dev dev on cmdLog.dev_id=dev.id\n" +
                " LEFT JOIN iot_dev_type type on type.id=dev.dev_type_id \n" +
                " where 1=1 \n" +
                //" where dev.id in (104,141,142,103,104,105,106)" + // debug
//                " and coll.is_get=1 \n" +

                " and resp_time is null \n" +
                " order by cmdLog.cmd_time ";

        try {
            List<GeneralDev> curDevs = new LinkedList<GeneralDev>();

            Map<Object, Map<String, Object>> dbObj =
                    sqlHelper.query(sql2GetDevs);

            Set<Map.Entry<Object, Map<String, Object>>> allDevEntrys = dbObj.entrySet();

            for(Map.Entry<Object, Map<String, Object>> enery:  allDevEntrys){
                Object key = enery.getKey();
                Map<String, Object> devInfo = enery.getValue();

                //String prol = (String)devInfo.get("prol");
                String cmd = (String)devInfo.get("cmd");
                String param = (String)devInfo.get("cmdParam");
                Integer cmdId = (Integer)devInfo.get("cmdId");

                GeneralDev dev = null;
                dev = DevFactory.getOrNew(devInfo,this.sqlHelper , this.dataLogService);

                try {
                    if(dev!= null)dev.cmd(cmdId, cmd, param);//启动指令
                } catch (Exception e) {
                    e.printStackTrace();
                }

                curDevs.add(dev);
            }


            this.preDevs  = curDevs;




        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args){
        test();
    }

    public static void test(){

        U.cachedThreadPool.execute(new CmdMainWork());

    }


}

